package net.sharpf2.api.interfaces;

import net.sharpf2.sfcore.client.Interface;
import net.sharpf2.api.Globals;
import net.sharpf2.api.game.Interfaces;
import net.sharpf2.api.types.Content;
import net.sharpf2.api.util.web.WorldData;

/**
 *
 * @author Piotr Gajdowski
 * @author Rick van Biljouw
 */
public class Game {
    
    /**
     * Returns all GUI components
     * @return
     */
    public static Interface[] getComponents() {
        return Globals.CLIENT.getInterfaceCache()[Globals.CLIENT.getGUIIndex()];
    }   
    
    /**
     * Checks if game screen is resizable or fixed.
     * @return true otherwise false.
     */
    public static boolean isInResizable() {
        Content<Interface> inventory = Interfaces.get(Constants.INVENTORY, 0);
        int x = inventory.getContent().getAbsoluteX();
        int y = inventory.getContent().getAbsoluteY();
        return x != 563 && y != 213;
    }
    
    /**
     * Gets the current world the player is playing in.
     * @return int world number.
     */
    public static int getCurrentWorld() {
        int world = -1;
        Content<Interface> inGame = Interfaces.get(Constants.FRIENDS_LIST, Constants.FRIENDS_LIST_WORLD);
        Content<Interface> inLobby = Interfaces.get(Constants.LOBBY, Constants.LOBBY_WORLD);
        if (inGame != null) {
            String[] text = inGame.getContent().getText().split("<br>");
            String[] retText = text[1].split(" ");
            world = Integer.parseInt(retText[1]);
        }
        if (inLobby != null) {
            String[] retText = inLobby.getContent().getText().split(" ");
            world = Integer.parseInt(retText[1]);
        }
        return world;
    }
    
    /**
     * Checks if current world is a members only world.
     * @return true if yes, otherwise false.
     */
    public static boolean inMemberWorld() {
        if (WorldData.lookup(getCurrentWorld(), 0).isMember()) {
            return true;
        }
        return false;
    }
    
    /**
     * Gets the username of the current logged in player.
     * @return String username.
     */
    public String getPlayerName() { // Credits to Cov.
        Content<Interface> chat = Interfaces.get(137, 55);
        return chat != null ? chat.getContent().getText().replace("<img=3>: <col=0000ff>", "") : null;
    }
    
    public static class Constants {
        public final static int INVENTORY = 149;
        public final static int FRIENDS_LIST = 550;
        public final static int FRIENDS_LIST_WORLD = 19;
        public final static int LOBBY = 910;
        public final static int LOBBY_WORLD = 11;
    }
}
